import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

import errorPage404 from "@/views/404.vue";

import YIZHANGTU from "@/views/yizhangtu/index.vue";
import foldList from "@/views/yizhangtu/basicFolder/foldList.vue";
import AuxiliaryLocation from "@/views/auxiliaryLocation/auxiliaryLocation.vue";
import Audit from "@/views/yizhangtu/applyAnalysis/Audit.vue";
import OnlineQuery from "@/views/yizhangtu/query/onlineQuery/index.vue";
import ToponymyLocation from "@/views/yizhangtu/query/toponymyLocation/index.vue";
import ProjectQuery from "@/views/yizhangtu/query/projectQuery/index.vue";

import { config } from "@/config";

const routes: Array<RouteRecordRaw> = [
  { path: "/", name: "login", redirect: "Login" },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login.vue"), // 登录页面
  },
  {
    path: "/portal",
    name: "Portal",
    component: () => import("@/views/portal/index.vue"), // 门户主页
  },
  {
    path: "/atlasReport",
    name: "AtlasReport",
    component: () => import("@/views/atlasReport/index.vue"), // 图集报告页面
  },
  {
    path: "/shareServe",
    name: "ShareServe",
    component: () => import("@/views/shareServe/index.vue"), //服务共享
  },
  // 嵌套路由    yizhangtu是一张图的一级路由
  {
    path: "/yizhangtu",
    name: "yizhangtu",
    meta: { title: "专题" },
    component: YIZHANGTU,
    children: [
      //folder
      {
        path: "/basic",
        name: "Basic",
        meta: { title: "专题目录" },
        component: foldList,
      },
      //选址
      {
        path: "/location",
        name: "location",
        meta: { title: "辅助选址" },
        component: AuxiliaryLocation,
      },
      {
        path: "/audit",
        name: "Audit",
        meta: { title: "合规审查" },
        component: Audit,
      },
      {
        path: "/online_query",
        name: "OnlineQuery",
        meta: { title: "在线查询" },
        component: OnlineQuery,
      },
      {
        path: "/project_query",
        name: "ProjectQuery",
        meta: { title: "项目查询" },
        component: ProjectQuery,
      },
      {
        path: "/toponymy_location",
        name: "ToponymyLocation",
        meta: { title: "地名地址" },
        component: ToponymyLocation,
      },
      //   404
      {
        path: "/:pathMatch(.*)*",
        name: "error-404",
        component: errorPage404,
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

//路由守卫
router.beforeEach((to, from, next) => {
  //判断本地是否有用户信息，实际需要判断是否有token
  console.log("跳转路径：", to.path);
  if (
    window.localStorage.getItem(config.system_prefix + "userInfo") ||
    to.path == "/login"
  ) {
    next();
  } else if (to.path !== "/login") {
    //跳转到登录页面
    next({
      name: "Login",
    });
  }
});

router.afterEach((to, from, next) => {
  console.log(to);
});

export default router;
